package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.ExecutionException;

@Transactional //多个数据源修改是需要添加事务标签，保证数据修改的一致性.
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public void save(ApplicationSaveParam saveParam) {
        Application application = new Application();
        BeanUtils.copyProperties(saveParam,application);
        //为申请单的状态进行赋值
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        //设置创建时间
        application.setCreateTime(new Date());
        //新增申请表单
        applicationMapper.insertApplication(application);
        //新增审核表单
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        //根据查询条件进行查询
        List<ApplicationVO> applicationVOList = applicationMapper.select(applicationQuery);
        //申请单中有审批人，需要遍历applicationVOList对象
        for (int i = 0; i < applicationVOList.size(); i++) {
            ApplicationVO applicationVO = applicationVOList.get(i);
            //为申请单赋值审批人
            applicationAuditUserList(applicationVO);
        }
        //TODO 审核通过后，分配车辆
        return applicationVOList;
    }

    @Override
    public void cancel(Long id) {
        //sql设计提供一个update(application)。为了扩展
        Application application = new Application();
        application.setId(id);
        //申请单状态改为撤销
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        //更新时间
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //撤销后，对应的审核信息要删除
        auditMapper.deleteByApplicationId(id);
    }
    //分配车辆
    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        //申请单数据
        Application application = new Application();
        //设置申请单id
        application.setId(applicationId);
        //设置分配的车辆id
        application.setVehicleId(vehicleId);
        //设置申请单状态为已分配
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        //更新修改时间
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //车辆数据
        Vehicle vehicle = new Vehicle();
        //设置车辆id
        vehicle.setId(vehicleId);
        //设置车辆为占用的状态
        vehicle.setStatus("2");
        vehicleMapper.update(vehicle);
    }
    //还车
    @Override
    public void back(Long applicationId, Long vehicleId) {
        //申请单数据
        Application application = new Application();
        //设置申请单id
        application.setId(applicationId);
        //设置分配的车辆id
        application.setVehicleId(vehicleId);
        //设置申请单状态为工单完成
        application.setStatus(ApplicationStatusEnum.END.getCode());
        //更新修改时间
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //车辆数据
        Vehicle vehicle = new Vehicle();
        //设置车辆id
        vehicle.setId(vehicleId);
        //设置车辆为空闲的状态
        vehicle.setStatus("1");
        vehicleMapper.update(vehicle);
    }

    //处理当前申请单的审批人相关信息
    private void applicationAuditUserList(ApplicationVO applicationVO) {
        //准备存储审批人姓名和id的集合
       List<String> auditUsernameList =  new ArrayList<>();
       List<Long> auditUserIdList =  new ArrayList<>();
       //根据申请单，查询对应的审批数据(可以有多个)
       List<AuditVO> auditVOS = auditMapper.selectAuditByApplicationId(applicationVO.getId());
       //判断auditVOS是否为空
       if(auditVOS==null)
          throw new ServiceException(StatusCode.OPERATION_FAILED);
        for (int i = 0; i < auditVOS.size(); i++) {
           //取出审批单对象
            AuditVO auditVO = auditVOS.get(i);
            Long userId = auditVO.getAuditUserId();
            //将userId赋值到auditUserIdList集合中
            auditUserIdList.add(userId);
            UserVO userVO = userMapper.selectByParentId(userId);
            //将审批人姓名存储到auditUsernameList集合中
            auditUsernameList.add(userVO.getUsername());
        }
        //将准备好的审批人和id的集合赋值到applicationVO中
        applicationVO.setAuditUserIdList(auditUserIdList);
        //存储审批人的姓名时，使用逗号隔开
        //准备一个工具，进行值与值之间的拼接   stringJoiner.add("zhangsan") stringJoiner.add("lisi")   zhangsan,lisi
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String username : auditUsernameList) {
            stringJoiner.add(username);
        }
        //将拼接好的审批人姓名赋值到applicationVO中
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
